package com.dll.pybig.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csvreader.CsvReader;
import com.dll.pybig.dao.PathMapper;
import com.dll.pybig.dao.StuCommentMapper;
import com.dll.pybig.pojo.Path;
import com.dll.pybig.pojo.StuCommentInfo;
import com.dll.pybig.pojo.StuSeatInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Service
@EnableTransactionManagement
public class StuCommentService {

    @Autowired
    PathMapper pathMapper;
    @Autowired
    StuCommentMapper stuCommentMapper;

    public List<StuCommentInfo> readCsv(){
        CsvReader reader = null;
        Path path = pathMapper.selectById(1l);
        List<StuCommentInfo> stuCommentInfos = new ArrayList<>();
        try {
            //第一个参数是座位信息的位置
            reader = new CsvReader(path.getOutput()+"simplifiedData.csv",',',Charset.forName("utf-8"));




            // 逐条读取记录，直至读完
            reader.readRecord();
            while (reader.readRecord()) {
                // 读一整行

                String rawRecord = reader.getRawRecord();
                if(!rawRecord.equals("")||!rawRecord.equals(",,")) {
                    // 读这行的第一列
                    stuCommentInfos.add(new StuCommentInfo(Long.valueOf(reader.get(0)),reader.get(1),reader.get(2)));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != reader) {
                reader.close();
            }
        }

        return stuCommentInfos;
    }

    public void insertDataBases(List<StuCommentInfo> commentInfos){

        for (StuCommentInfo commentInfo : commentInfos) {
            stuCommentMapper.insert(commentInfo);

        }
    }

    public void updateData(List<StuCommentInfo> commentInfos){
        UpdateWrapper<StuCommentInfo> w = new UpdateWrapper<>();

        stuCommentMapper.delete(null);
        insertDataBases(commentInfos);
    }
    public Object searchInfo(String name,Integer pages){
        QueryWrapper<StuCommentInfo> s = new QueryWrapper<>();
        s.like("stuName",name);
        Page<StuCommentInfo> c = new Page<StuCommentInfo>(pages,5);
         return stuCommentMapper.selectPage(c,s);

    }
}
